<template>
  <CompOperate
      :field="field"
      :designer="designer"
      :design-state="designState"
      :parent-list="parentList"
      :parent-widget="parentWidget"
      :sub-form-row-id="subFormRowId"
      :sub-form-row-index="subFormRowIndex"
      :sub-form-col-index="subFormColIndex"
      :index-of-parent-list="indexOfParentList"
      :display-style="field['options'].displayStyle"
  >
    <div class="scene-DRC-trends-widget" :style="getStyle('head')">
      <!--模板信息-->
      <div class="doctor-list-wrap" :key="widgetKey">
        <component
            :field="field"
            :devType="devType"
            :is="componentName"
            :doctorData="getRoomDoctorInfo"
        />
      </div>
    </div>
  </CompOperate>
</template>

<script>
import CompOperate from "@c/public/form-designer/devType/modules/compOperate.vue";
import {buildParentApi} from "@/utils/designer/util";
import emitter from "@/utils/designer/emitter";
import mixin from "@sc/templateDetails/modules/homeMiddle/fieldMixin";
import {v4 as uuid} from "uuid";
import {pxToVw} from "@/utils/common";
import ScrollX from "@c/public/seamlessScroll/ScrollX.vue";
import Template from "@sc/template/index.vue";
import trends3DoctorList1 from './trends3Comps/trends3DoctorList-1.vue';
import trends3DoctorList2 from './trends3Comps/trends3DoctorList-2.vue';

let buildParent = buildParentApi();
export default {
  name: 'scene-DRC-trends3-widget',

  componentName: "FieldWidget", //必须固定为FieldWidget，用于接收父级组件的broadcast事件

  mixins: [emitter, mixin],

  components: {
    Template,
    ScrollX,
    CompOperate,
    trends3DoctorList1,
    trends3DoctorList2
  },

  props: {
    widget: Object,
    devType: {
      type: Boolean,
      default: false,
    },
    field: Object,
    parentWidget: Object,
    parentList: Array,
    indexOfParentList: Number,
    designer: Object,
    designState: {
      type: Boolean,
      default: false,
    },
    subFormRowIndex: {
      /* 子表单组件行索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormColIndex: {
      /* 子表单组件列索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormRowId: {
      /* 子表单组件行Id，唯一id且不可变 */ type: String,
      default: "",
    },
    headTitle: {
      type: String,
      default: "",
    },
    visibleTitle: {
      type: Boolean,
    },
    //prop传入的表单JSON配置
    parentApi: {
      type: Object,
      default: () => buildParentApi()
    },
  },

  data() {
    return {
      number: 1,
      widgetKey: uuid(),
      optionsInfo: Object(),
      header: require("@a/images/design/header.jpeg"),
      pause: require("@a/images/design/pause.png"),
    }
  },

  computed: {
    // 设置的行内样式
    getStyle() {
      return function (val) {
        const self = this;
        let info = (self.optionsInfo) || Array();
        let Obj = Object();
        switch (val) {
          case 'head':
            Reflect.set(Obj, 'height', pxToVw(info.headHeight));
            Reflect.set(Obj, 'backgroundColor', `${(info.backgroundColor)}`);
            return Obj;
          case 'picture':
            Reflect.set(Obj, 'height', pxToVw(info.imageHeight));
            Reflect.set(Obj, 'width', pxToVw(info.imageWidth));
            Reflect.set(Obj, 'borderRadius', pxToVw(info.imageRadius));
            return Obj;
          case 'deptName':
            Reflect.set(Obj, 'fontSize', pxToVw(info.deptNameFontSize));
            return Obj;
          case 'roomName':
            Reflect.set(Obj, 'fontSize', pxToVw(info.roomNameFontSize));
            return Obj;
        }
      }
    },

    // 获取全量医患信息
    getRoomDoctorInfo() {
      let listInfo = Array()
      let devType = (this.devType);
      let {doctorNumber = 1} = this.optionsInfo;
      let roomDoctorInfo = this.$store.getters['getRoomDoctorInfo'];
      if (devType) {
        listInfo = roomDoctorInfo.apiData;
      } else {
        listInfo = roomDoctorInfo.mockData
      }
      if (!!(listInfo?.length)) {
        if (devType) {
          if (listInfo?.length >= 7) {
            return listInfo.slice(0, 6);
          } else {
            return listInfo.slice(0);
          }
        } else {
          return listInfo.slice(0, doctorNumber);
        }
      } else {
        return Array(1).fill({});
      }
    },

    // 过号患者信息
    passPatientInfos() {
      let devType = (this.devType);
      let WaitingPatientList = this.$store.getters['getPassPatientInfo'];
      if (devType) {
        return WaitingPatientList.apiData;
      } else {
        return WaitingPatientList.mockData;
      }
    },

    // 加载对应的动态组件
    componentName() {
      let doctorInfo = (this.getRoomDoctorInfo);
      console.log(doctorInfo);
      if (doctorInfo?.length) {
        return `trends3DoctorList${doctorInfo?.length}`
      } else {
        return `trends3DoctorList1`
      }
    },

  },

  created() {
    this.registerToRefList();
    this.initEventHandler();
    this.handleOnCreated();
  },

  mounted() {
    this.handleOnMounted();
    // this.executeMethod('roomDoctorInfo',resp)
  },

  methods: {
    // 接收父组件传递过来的数据
    executeMethod(key, data) {
      switch (key) {
        case "roomDeptInfo":
          // console.log(`科室信息-${data}`)
          this.states = true;
          this.$store.dispatch('setRoomDeptInfo', this.$lodash.cloneDeep(data))
          break
        case "roomDoctorInfo":
          // console.log(`医生信息-${data}`);
          this.widgetKey = uuid();
          this.$store.dispatch('setRoomDoctorInfo', this.$lodash.cloneDeep(data))
          break
      }
    },

    // 滚动结束
    handleScrollEnd() {
    },
  },

  watch: {
    field: {
      handler: function (val) {
        this.widgetKey = uuid();
        let key = (val.options.config)
        this.optionsInfo = val.options[key];
      },
      deep: true,
      immediate: true
    }
  },

  destroyed() {
    this.unregisterFromRefList();
  },

}
</script>


<style scoped lang="scss">
//科室&诊室通用样式
.scene-DRC-trends-widget {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  padding: 20px 30px;
  box-sizing: border-box;
  color: #333;
  height: 1670px;
  font-size: 40px;
  background: transparent;
  overflow: hidden;

  .doctor-list-wrap {
    background: transparent;
    flex-shrink: 0;
    height: calc(100%);
    box-sizing: border-box;
    overflow: hidden;
  }
}
</style>
